package com.briup;

import java.util.concurrent.TimeUnit;

/**
 * 锁
 *
 * @auther thb
 * @date 2021/9/5
 */
class T4_Synchronized {

    public static void main(String[] args) {
        T4_sync t1 = new T4_sync();
        T4_sync t2 = new T4_sync();
        t1.start();
        t2.start();
    }

}

class T4_sync extends Thread {

    private Integer count = 20;
    private Integer num = 0;
    private Boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            synchronized (this) {
                if (count <= 0) {
                    flag = false;
                    System.out.println("已经没有票了");
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                num++;
                count--;
                System.out.println(Thread.currentThread().getName() + " 抢到第" + num + "票，还剩"+count+"张票。");
            }
        }
    }

    public synchronized void show() {

//        synchronized (this) {
//
//        }
    }

}